package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    //添加
    @Override
    public void savewithFlavor(DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(f -> f.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }
    //根据id查询回显数据
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        return dishMapper.getByIdWithFlavor(id);
    }
    //修改
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        //先删口味再加
        dishFlavorService.lambdaUpdate()
                .eq(DishFlavor::getDishId,dishDto.getId())
                .remove();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //设置口味的dishId
        flavors.forEach(f->f.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    //分页查询
    @Override
    public Page<DishDto> pageDishDto(Integer page, Integer pageSize, String name) {
        Page<DishDto> pageInfo=new Page(page,pageSize);
        return dishMapper.pageDishDto(pageInfo,name);
    }
    //批量删除以及口味
    @Override
    public void deleteWithFlavor(List<Long> ids) {
        //判断状态是否在售，是则取消删除
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids)
                          .eq(Dish::getStatus,1);
        int count = this.count(lambdaQueryWrapper);
        if (count>0){
            throw new CustomerException("菜品为起售状态，无法删除");
        }
        //否就执行删除方法
        this.removeByIds(ids);
        //删除相对应的口味
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.in(DishFlavor::getDishId,ids);
        //删除
        dishFlavorService.remove(lqw);
    }

    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {
        return dishMapper.listDishDto(categoryId,name);
    }
    //移动端单击展示菜品详细数据
    @Override
    public List<DishDto> getDishBysetmealId(Long setmealId) {
        return dishMapper.getDishBysetmealId(setmealId);
    }
}


